package game.metalmax.module.world
{
	/**
	 * WorldProxy.as
	 * Yin Hao
	 * @version 1.0.0
	 * 
	 * Description.
	 * 
	 * Copyright (c) 2012 Yin Hao
	 * 
	 * Permission is hereby granted, free of charge, to any person obtaining a copy
	 * of this software and associated documentation files (the "Software"), to deal
	 * in the Software without restriction, including without limitation the rights
	 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	 * copies of the Software, and to permit persons to whom the Software is
	 * furnished to do so, subject to the following conditions:
	 * 
	 * The above copyright notice and this permission notice shall be included in
	 * all copies or substantial portions of the Software.
	 * 
	 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	 * THE SOFTWARE.
	 */
	
	import game.metalmax.DataLocator;
	import game.metalmax.config.GameConfig;
	import game.metalmax.module.server.GateWay;
	import game.metalmax.module.server.ServerProxy;
	import game.metalmax.packet.world.ActionItemRequest;
	import game.metalmax.packet.world.EnterSceneRequest;
	import game.metalmax.packet.world.EnterSceneResponse;
	import game.metalmax.packet.world.MoveHeroRequest;
	import game.metalmax.packet.world.PObjectHolder;
	import game.metalmax.packet.world.PPoint;
	import game.metalmax.packet.world.TransferOffRequest;
	import game.metalmax.packet.world.TransferOffResponse;
	import game.metalmax.packet.world.TransferOnRequest;
	
	import showtime.framework.engine.map.rpg.ObjectHolder;
	import showtime.framework.events.GameEvent;
	import showtime.framework.events.MessageDispatcher;
	import showtime.framework.mvc.patterns.proxy.Proxy;

	
	public class WorldProxy extends Proxy
	{
		public function WorldProxy()
		{
			super(String(WorldProxy));
		}
		
		override public function onRegister():void
		{
			super.onRegister();
			
			GateWay.register(Header.ENTER_SCENE_S2C, enterSceneS2C);
			GateWay.register(Header.TRANSFER_OFF_S2C, transferOffS2C);
		}
		
		override public function onRemove():void
		{
			super.onRemove();
		}
	
		public function enterSceneC2S(mapID:String):void
		{
			var requset:EnterSceneRequest = new EnterSceneRequest();
			requset.mapID = mapID;
			requset.email = DataLocator.getInstance().email;
			ServerProxy.sendPacketToServer(Header.ENTER_SCENE_C2S, requset);
		
		}
		
		private function enterSceneS2C(packet:EnterSceneResponse):void
		{			
			var objects:Array = [];
			var object:ObjectHolder = null;
			for each (var pObject:PObjectHolder in packet.objects)
			{
				object = new ObjectHolder();
				object.type = pObject.type;
				object.height = pObject.height;
				object.name = pObject.name;
				object.x = pObject.position.x * GameConfig.MAP_TILE_WIDTH;
				object.y = (pObject.position.y) * GameConfig.MAP_TILE_HEIGHT;
				object.properties = {};
				object.properties["animationConfigID"] = pObject.animationConfigID;
				object.properties["animationName"] = pObject.animationName;
				object.properties["graphicID"] = pObject.graphicID;
				object.properties["solid"] = pObject.solid;
				object.properties["name"] = pObject.name;
				object.properties["direction"] = pObject.direction;
				object.properties["subType"] = pObject.subType;
				object.properties["visible"] = pObject.visible;
				object.properties["id"] = pObject.id;
				objects.push(object);
			}
	
			MessageDispatcher.getInstance().dispatchEvent(new GameEvent(GameEvent.SCENE_OBJECT_ADD, objects));
		}
		
		public function actionItemC2S(item:Object):void
		{
			var request:ActionItemRequest = new ActionItemRequest();
			request.mapID = WorldManager.getInstance().nowMapID;
			request.itemID = item.id;			
			
			ServerProxy.sendPacketToServer(Header.ACTION_ITEM_C2S, request);
		}
		
		public function moveHeroC2S(x:int , y:int):void
		{
			var request:MoveHeroRequest = new MoveHeroRequest();
			request.position = new PPoint();
			request.position.x = x;
			request.position.y = y;
			request.email = DataLocator.getInstance().email;
			
			ServerProxy.sendPacketToServer(Header.MOVE_HERO_C2S, request);
		}

		public function transferOnC2S(heroID:String, animationConfigID:String, animationName:String, graphicID:String, vehicleID:String):void
		{
			var request:TransferOnRequest = new TransferOnRequest();
			request.heroID = heroID;
			request.animationConfigID = animationConfigID;
			request.animationName = animationName;
			request.graphicID = graphicID;
			request.vehicleID = vehicleID;
			
			ServerProxy.sendPacketToServer(Header.TRANSFER_ON_C2S, request);
		
		}
		
		private function transferOnS2C():void
		{
			
		}
		
		public function transferOffC2S(heroID:String, x:int, y:int):void
		{
			var request:TransferOffRequest = new TransferOffRequest();
			request.heroName = heroID;
			request.position = new PPoint();
			request.position.x = x / GameConfig.MAP_TILE_WIDTH;
			request.position.y = y / GameConfig.MAP_TILE_HEIGHT;
			
			ServerProxy.sendPacketToServer(Header.TRANSFER_OFF_C2S, request);
		}
		
		private function transferOffS2C(packet:TransferOffResponse):void
		{
			
		}
		
		
	}
}